<?php
include_once("DataHelper.php");
//include_once("TaiKhoanDTO.php");

class TaiKhoanDAO
{
	public static function ParseToTaiKhoanDTO($row)
	{
		$taikhoanDTO = new TaiKhoanDTO();
		$taikhoanDTO->tk_username = $row["tk_username"];
		$taikhoanDTO->tk_password = $row["tk_password"];
		$taikhoanDTO->tk_ngay_tao = $row["tk_ngay_tao"];
		$taikhoanDTO->tk_email = $row["tk_email"];
		$taikhoanDTO->tk_ho_ten = $row["tk_ho_ten"];
		$taikhoanDTO->tk_gioi_tinh = $row["tk_gioi_tinh"];
		$taikhoanDTO->tk_dia_chi = $row["tk_dia_chi"];
		$taikhoanDTO->tk_dien_thoai = $row["tk_dien_thoai"];
		$taikhoanDTO->tk_avatar = $row["tk_avatar"];
		$taikhoanDTO->tk_tinh_trang = $row["tk_tinh_trang"];
		$taikhoanDTO->tk_loai = $row["tk_loai"];
		return $taikhoanDTO;
	}
	
	public static function LayUserTheoUsername($username)
	{
		$sql = "SELECT * FROM tai_khoan WHERE tk_username= '$username'";
		
		$result = QuerySQL($sql);
		$numrow = mysql_num_rows($result);
		if($numrow	> 0)
		{
			$row = mysql_fetch_assoc($result);
			$taikhoanDTO = TaiKhoanDAO::ParseToTaiKhoanDTO($row);
			return $taikhoanDTO;
		}
		return null;
	}
	
	public static function KiemTraEmailHopLe($email)
	{
		$sql = "SELECT * FROM tai_khoan WHERE tk_email= '$email'";
		
		$result = QuerySQL($sql);
		$numrow = mysql_num_rows($result);
		//echo "<br/> DAO - SO TRA VE : ".$numrow;
		if($numrow	> 0)
		{
			return true;
		}
		return false;
	}
	
	public static function CapNhatTaiKhoanTheoUsername($taikhoanDTO,$username)
	{
		$str = $taikhoanDTO->tk_password . $taikhoanDTO->tk_username;
		$taikhoanDTO->tk_password = md5($str);
		$sqlString = "UPDATE tai_khoan SET 
						tk_password = '$taikhoanDTO->tk_password',
						tk_email = '$taikhoanDTO->tk_email',
						tk_ho_ten = '$taikhoanDTO->tk_ho_ten',
						tk_dia_chi = '$taikhoanDTO->tk_dia_chi',
						tk_dien_thoai = '$taikhoanDTO->tk_dien_thoai',
						tk_avatar = '$taikhoanDTO->tk_avatar'
						WHERE tk_username = '$username'";
		$result = QuerySQL($sqlString);
		return $result;
		
	}
	public static function DanhSachTaiKhoanNguoiBan()
	{
		$sql = "SELECT * FROM tai_khoan WHERE tk_loai= 0";
		$result = QuerySQL($sql);
		$result = QuerySQL($sql);
		$arraySpDTO = NULL;
		if($result !=NULL)
		{
			$i = 0;
			while($row = mysql_fetch_array($result))
			{
				$spDTO = TaiKhoanDAO::ParseToTaiKhoanDTO($row);
				$arraySpDTO[$i] = $spDTO;
				$i = $i + 1;
			}
			StopConnection();
			return $arraySpDTO;
		}
		return NULL;
	}
	
	public static function DanhSachTaiKhoanNguoiMua()
	{
		$sql = "SELECT * FROM tai_khoan WHERE tk_loai= 1";
		$result = QuerySQL($sql);
		$result = QuerySQL($sql);
		$arraySpDTO = NULL;
		if($result !=NULL)
		{
			$i = 0;
			while($row = mysql_fetch_array($result))
			{
				$spDTO = TaiKhoanDAO::ParseToTaiKhoanDTO($row);
				$arraySpDTO[$i] = $spDTO;
				$i = $i + 1;
			}
			StopConnection();
			return $arraySpDTO;
		}
		return NULL;
	}
	public static function CapNhatTrangThaiNguoiDung($TrangThai,$Username)
	{
	//echo $TrangThai;
	//echo $Username;
	//echo "baoloc";
		$sql = "update tai_khoan set tk_tinh_trang = '$TrangThai' WHERE tk_username = '$Username'";
		//echo $sql;
		$result = QuerySQL($sql);
		//StopConnection();
		if($result >0)
		{
			return true;
		}
		else
		return false;
	}
	
	public static function LayLoaiTaiKhoan($Username)
	{
		$sql = "SELECT * FROM tai_khoan WHERE tk_username = '$Username'";
		$result = QuerySQL($sql);
		$result = QuerySQL($sql);
		$arraySpDTO = NULL;
		if($result !=NULL)
		{
			while($row = mysql_fetch_array($result))
			{
				$spDTO = TaiKhoanDAO::ParseToTaiKhoanDTO($row);			}
			StopConnection();
			return $spDTO;
		}
		return NULL;
	}
	
	public static function XoaTaiKhoan($Username)
	{
		$sql = "delete from tai_khoan WHERE tk_username = '$Username'";
		//echo $sql;
		$result = QuerySQL($sql);
		//StopConnection();
		if($result >0)
		{
			return true;
		}
		else
		return false;
	}
}
?>